<template>
  <div id="map" class="map" ref="rootmap"></div>
</template>

<script>
import 'ol/ol.css';
import Circle from 'ol/geom/Circle';
import Feature from 'ol/Feature';
import GeoJSON from 'ol/format/GeoJSON';
import Map from 'ol/Map';
import View from 'ol/View';
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style';
import { OSM, Vector as VectorSource } from 'ol/source';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
import shaoxingCity from './shaoxingCity.json';

const vectorSource = new VectorSource({
  features: new GeoJSON().readFeatures(shaoxingCity),
});

vectorSource.addFeature(new Feature(new Circle([5e6, 7e6], 1e6)));
const vectorLayer = new VectorLayer({
  source: vectorSource,
  style: new Style({
    stroke: new Stroke({
      color: 'yellow',
      width: 1,
    }),
    fill: new Fill({
      color: 'rgba(255, 255, 0, 0.1)',
    }),
  }),
});

let map = null;
export default {
  data() {
    return {};
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      map = new Map({
        layers: [
          new TileLayer({
            source: new OSM(),
          }),
          vectorLayer,
        ],
        target: 'map',
        view: new View({
          center: [120, 30],
          zoom: 8,
          projection: 'EPSG:4326',
        }),
      });
      // const url = 'http://8.136.236.190:30000/geoserver/gwc/service/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=zhejiang:tb_cereal_2021&STYLE=&TILEMATRIX=EPSG:4326:{z}&TILEMATRIXSET=EPSG:4326&FORMAT=application/vnd.mapbox-vector-tile&TILECOL={x}&TILEROW={y}';

      // var mapExtent = map.getView().calculateExtent(map.getSize());
      // var point = getCenter(mapExtent);
      // debugger;
      // setTimeout(() => {
      //   map.getView().setCenter(point); //设置当前地图的显示中心位置
      // }, 100);
    },
  },
};
</script>

<style></style>
